package com.demo.spring.jms;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

@Component
public class JmsQueueSender {
	
	@Autowired
	private JmsTemplate jmsTemplate;
	
	
	public void setConnectionFactory(ConnectionFactory factory){
		this.jmsTemplate = new JmsTemplate(factory);
	}
	
	public void simpleSend(String queueName){
		this.jmsTemplate.send(queueName, new MessageCreator() {
			@Override
			public Message createMessage(Session session) throws JMSException {
				return session.createTextMessage("hello queue world");
			}
		});
	}
	
	public Message consumee(String queueName){
		Message message = jmsTemplate.receive(queueName);
		return message;
	}
}
